﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace LinqtoCRM.Tests
{
    [TestFixture]
    public class ValidatingTests
    {
        private CrmDataContext crm;

        [TestFixtureSetUp]
        public void FixtureSetup()
        {
            crm = new CrmDataContext(
                new ValidatingNonExecutingCrmService("FetchXml.xsd")
            );
        }

        [Test]
        public void TestAllContactsQuery()
        {
            var result = from c in crm.Contacts
                         select c;
            result.ToList();
        }

        [Test]
        public void TestAllAccountsQuery()
        {
            var result = from c in crm.Accounts
                         select c;
            result.ToList();
        }

        [Test]
        public void TestSimpleStringWhere()
        {
            var result = from c in crm.Contacts
                         where c.firstname == "Hank"
                         select c;
            result.ToList();
        }

        [Test]
        public void TestJoin()
        {
            var result = from a in crm.Accounts
                         join c in crm.Contacts
                         on a.accountid.Value equals c.parentcustomerid.Value
                         select c;
            result.ToList();
        }

        [Test]
        public void TestOrderBy()
        {
            var result = from c in crm.Contacts
                         orderby c.firstname
                         select c;
            result.ToList();
        }

        [Test]
        public void TestJoinOrderBy()
        {
            var result = from c in crm.Contacts
                         join a in crm.Accounts
                         on c.parentcustomerid.Value equals a.accountid.Value
                         orderby c.firstname
                         select c;
            result.ToList();
        }

        [Test]
        public void TestAllContactsQueryNew()
        {
            var result = from c in crm.Contacts
                         select new { c.firstname };
            result.ToList();
        }

        [Test]
        public void TestAllAccountsQueryNew()
        {
            var result = from c in crm.Accounts
                         select new { c.name };
            result.ToList();
        }

        [Test]
        public void TestSimpleStringWhereNew()
        {
            var result = from c in crm.Contacts
                         where c.firstname == "Hank"
                         select new { c.firstname };
            result.ToList();
        }

        [Test]
        public void TestJoinNew()
        {
            var result = from a in crm.Accounts
                         join c in crm.Contacts
                         on a.accountid.Value equals c.parentcustomerid.Value
                         select new { a.name };
            result.ToList();
        }

        [Test]
        public void TestOrderByNew()
        {
            var result = from c in crm.Contacts
                         orderby c.firstname
                         select new { c.firstname };
            var r = result.ToList();
        	r.Count();
        }

        [Test]
        public void TestJoinOrderByNew()
        {
            var result = from c in crm.Contacts
                         join a in crm.Accounts
                         on c.parentcustomerid.Value equals a.accountid.Value
                         orderby c.firstname
                         select new { c.firstname };
            result.ToList();
        }

        [Test]
        public void TestHugeWhere()
        {
            var result = from c in crm.Contacts
                         where
                            c.firstname == "Scott" &&
                            c.lastname == "Scott" &&
                            c.jobtitle == "Developer" &&
                            c.managername == "scott" &&
                            c.emailaddress2 == "mail@gmail.com" &&
                            c.emailaddress1 == "mail@gmail.com" &&
                            c.emailaddress3 == "mail@gmail.com" ||
                            c.address1_city == "London"
                         select c;
            result.ToList();
        }

        [Test]
        public void TestDistinct()
        {
            var result = (from c in crm.Contacts
                          select c).Distinct();
            result.ToList();
        }

        [Test]
        public void TestJoinDistinct()
        {
            var result = (from c in crm.Contacts
                          join a in crm.Accounts
                          on c.parentcustomerid.Value equals a.accountid.Value
                          select new { a.name }).Distinct();
            result.ToList();
        }

        [Test]
        public void TestOrderbyDistinct()
        {
            var result = (from c in crm.Contacts
                          orderby c.firstname
                          select c.firstname).Distinct();
            result.ToList();
        }

    }
}
